{extend name='self@public/none'}
{block name="main"}
<style>
    .banner_a{
        /*border:1px solid red;*/
        position: absolute;
        cursor: pointer;
    }
    #file{
        width: 15px;
        height: 25px;
    }
    body{
        height:95%;
    }
</style>
<div class="log-block">

    <div id="up">
        <a class="banner_a" href="javascript:void(0);">上传附件
        </a>
        <input type="file" id="file" style="opacity: 0;cursor: pointer;" name="upload">
    </div>

    <table id="table" style="margin-top:10px;"></table>
    <input type="hidden" name="is_look" value="{$Think.get.is_look}">
    <input type="hidden" name="size" value="{$Think.get.size}">
    <input type="hidden" name="title" value="{$Think.get.title}">
</div>
{/block}
{block name="script"}
<script>
    $(function(){
        if($("[name='is_look']").val() == "yes"){
            $(".banner_a").hide();
        }
    });
    $('#table').bootstrapTable({
        url:'/self/upload/data_list',
        pagination:false,
        columns: [
            { field: 'file_name', title: '文件名'},
            { field: 'file_size', title: '文件大小',formatter:func.fileSize },
           // { field: 'file_type', title: 'Type' },
           // { field: 'file_ext', title: 'Ext' },
            { field: '', title: '操作', formatter:function(value,row,index){
                    var html = '';
                    let type = row.file_type;
                    let operate = '下载';
                    if(type.substr(0,5)==='image'){
                        operate = '查看';
                    }
                    html= "<a href='" + row.file_url + "' target='_blank'>"+operate+"</a>";
                    html += '<span class="line_span">&nbsp;&nbsp; || &nbsp;&nbsp;</span> ';
                    html += "<a data-del='" + row.id + "' target='_blank'>删除</a>";
                    return html;
                } },
            // {field:'',title:'图片',formatter:function(value,row,col)
            //     {
            //         let type = row.file_type;
            //         if(type.substr(0,5) === 'image')
            //         {
            //             return `<img src="${row.file_url}" style="width:80px;height:80px;" alt="">`
            //         }
            //         return '';
            //     }}
        ],
        queryParams: function (params) {
            params.rel = func.urlParam('rel');
            return params;
        },onLoadSuccess: function (res) {
            if($("[name='is_look']").val() == "yes"){
                //$("[name='upload']").hide();
                $("[data-del]").hide();
                $(".line_span").hide();
            }
        }
    });
    function bind() {
        $("#table").bootstrapTable("refresh", { silent: false });
    }
    //上传文件
    $("#up").on("change","[name=upload]",function(){
        var size = $('input[name=size]').val();
        var rel = func.urlParam('rel');///
        var fileObj = $(this)[0];
        var index = layer.load(0, { shade:0.2 });
        for(var i=0;i<fileObj.files.length;i++){
            var file = fileObj.files[i];
            var form = new FormData();
            form.append("file", file);
            form.append("rel", rel);
            form.append("size", size);
            var xhr = new XMLHttpRequest();
            xhr.open("post", "/self/upload/upfile", true);
            xhr.onload = function (e) {
                var html = e.currentTarget.responseText;
                var obj = JSON.parse(html);
                if(obj.code == 0) {
                    console.log(obj);
                    bind();
                } else {
                    layer.msg(obj.text, {icon: 5});
                }
            };
            xhr.onloadend = function (e) {
                layer.close(index);
            }
            xhr.send(form);
        }
        //accept=".jpg,.jpeg,.gif,.png,.pdf,.txt,.xls,.xlsx,.doc,.docx,.ppt,.pptx,.zip,.rar"
        $(this).remove();
        $("#up").html('');
        $("#up").append('<a class="banner_a" href="javascript:void(0);">Upload File</a><input type="file" id="file" style="opacity: 0;cursor: pointer;" name="upload">');

    });

    $(document).on('click','.banner_a',function(){
        $("#file").trigger("click");
    });
    //删除
    $("#table").on('click', '[data-del]',function(){
        var id = $(this).data('del');
        layer.confirm("您确定要删除此附件吗?",function()
        {
            var lindex = layer.load(2 ,{ icon: 2 ,shade: 0.1 });
            //提交数据
            frame.ajax({
                url:"/self/upload/delfile",
                type:"post",
                data:{id:id}
            }).then(function(data){
                layer.msg(data.text,{icon:1});
                if(data.code == 0){
                    bind();
                }
            });
        })

    });

</script>

{/block}